# SPDX-License-Identifier: GPL-2.0-only
# Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.

dhlibs-y += libdh-linux

# libdh-linux sources
libdh-linux-objs   := libdh_extralib.o libdh_linux.o liblinux_symbols.o
libdh-linux-deps   := $(obj)/libdh_linux.lds

ifeq ($(CONFIG_LIBLINUX_HOTPLUG),y)
libdh-linux-objs += cpuhp.o
cpuhp-ccflags += ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}
endif

ifeq ($(CONFIG_LIBLINUX_EXTRACT_KALLSYMS),y)
libdh-linux-objs += load_kallsyms/extract_kallsyms.o
load_kallsyms/extract_kallsyms-ccflags += ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}
endif

libdh-linux-objs += cred.o
cred-ccflags += ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}

libdh-linux-objs += page.o
page-ccflags += ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}

libdh-linux-objs += default_affinity.o
default_affinity-ccflags += ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}

ifeq ($(CONFIG_LIBLINUX_RSVD_HIGHMEM), y)
libdh-linux-objs += highmem.o
highmem-ccflags += ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}
endif

ifeq ($(CONFIG_LIBLINUX_CDC),y)
liblinux_cdc-ccflags += ${LINUXINCLUDE}
libdh-linux-objs += liblinux_cdc.o
liblinux_cdc-ccflags += -include $(srctree)/include/linux/export.h
cred-ccflags += -DENABLE_CURRENT_MM
endif

ccflags-y += -DCONFIG_DEVHOST_FS_TRANSFS=1
ccflags-$(CONFIG_NET) += -DCONFIG_DEVHOST_NETLINK=1
ccflags-$(CONFIG_STACKTRACE) += -DLNXBASE_HELPER_ENABLE_BACKTRACE=1
ccflags-$(CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE) += -O2

libdh_linux-ccflags += -DLNXBASE_HELPER_WITH_MODULE_NS -DLNXBASE_HELPER_ENABLE_TIMESYNC
liblinux_symbols-ccflags += -include $(srctree)/include/linux/export.h

libdh-linux-ldflags += \
	     -Wl,--whole-archive ${KBUILD_VMLINUX_OBJS} -Wl,--no-whole-archive \
	     -Wl,--start-group ${KBUILD_VMLINUX_LIBS} -Wl,--end-group \
	     -Wl,-T$(obj)/libdh_linux.lds \
	     -Wl,--version-script=$(srctree)/$(src)/libdh_linux.map \
	     -ldh-lnxbase

# link extralib if required
ifneq ($(CONFIG_SAMPLE_LDK_EXTRALIBS),"")
ifeq ($(LLVM),)
$(eval HMSDKSYSROOTPATH ?= $(shell $(CC) -print-sysroot))
endif
libdh-linux-extralibs := $(addprefix ${HMSDKSYSROOTPATH}/lib/modules/linux-${KERNELRELEASE}/lib/, \
			 $(subst ",, $(CONFIG_SAMPLE_LDK_EXTRALIBS)))
endif

# common flags
ccflags-y += -fstack-protector-strong -Werror -g \
	   -include $(objtree)/include/generated/autoconf.h \
	   -Wno-pointer-sign -std=gnu99
ldflags += -Wl,-z,relro,-z,now -Wl,-z,noexecstack \
	   -Wl,--hash-style=gnu -Wl,--build-id

include $(src)/Makefile.libs
